import { defineStore } from 'pinia'

export const userStore = defineStore({
  id: 'user', // id必填，且需要唯一
  state: () => {
    return {
				//登录状态
     		loginStatus:false,
     		//token
     		token:null,
     		//用户信息(昵称/头像)
     		userInfo:{},
    }
  },
  // actions 用来修改 state
  actions: {
      //登录后保存用户信息
			login(userInfo){
				this.userInfo = userInfo;
				this.loginStatus = true;
				this.token = userInfo.token;
			},
			// 退出登录
			loginOut(){
				this.loginStatus = false;
				this.userInfo={};
				this.token=null;
				localStorage.removeItem('user')
			}
  },
	persist: {
	  enabled: true,
	  strategies: [
	    {
				key:'user',
	      storage: localStorage,
	      paths: ['loginStatus', 'token','userInfo']
	    }
	  ]
	}
})
